<?php

/*
 * This class is used for as Model for Company table 
 */

class Model_Company extends Model_Base {

    protected $_name = 'tbl_company';
    protected $_primaryKey = 'com_id';

    /**
     * Function to return different units of the Company from the uom table
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    // static value for company type
    public function getcompanytypeArray() {
        return array(
            '' => '-- Select Type --',
            '1' => 'Contractor',
            '2' => 'Customer'
        );
    }

    
    
    //get country data
    public function getcountryArray() {
        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchall();
        $finalArray[''] = '';
        foreach ($countryArray as $com) {
            $finalArray[$com['country_id']] = $com['country_name'];
        }
        return $finalArray;
    }

    /**
     * Function to return different status of the Company
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
           # '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('com' => 'tbl_company'))
                ->setIntegrityCheck(false)
                #->join(array('child' => 'tbl_list'), "child.child_id = com.com_type")
                ->join(array('country' => 'tbl_country'), "country.country_id = com.com_country")
                ->join(array('login' => 'tbl_login'), "login.lgn_id = com.updated_lgn_id")
                ->where('com_id = ?', $id)
                ->where('com_status != 2');
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get companys
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchCompany($where = "", $limit = "", $offset = "0", $order = 'com_id', $sort = 'ASC' ) {
        $select = $this->select()
                ->from(array('com' => 'tbl_company'), array('*', 'company_type' => "IF(com_type = 1,'Contractor','Customer')"))
                ->setIntegrityCheck(false)
                # ->join(array('child' => 'tbl_list'), "child.child_id = com.com_type")
                ->where('com_status != 2');
        if ($where != '') {
            $select->where($where);
        }
       
        
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; 
        $data = parent::fetchAll($select);
        
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total rows
     * @return int
     */
    public function getCompanyCount($where) {
        $select = $this->select()
                ->from(array('com' => 'tbl_company'), Array('cnt' => 'count(*)', 'company_type' => "IF(com_type = 1,'Contractor','Customer')"))
                ->setIntegrityCheck(false)
                # ->join(array('child' => 'tbl_list'), "child.child_id = com.com_type")
                ->where('com_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete companys
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteCompany($prdtArray) {
        $config = Zend_Registry::get('config');
        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_company) {
                $companyData = Array();
                $companyData = $this->fetchEntryById($_company);

                if (count($companyData) > 0) {
                    $data = array('com_status' => '2');
                    $this->update($data, 'com_id =' . (int) $_company);
                    //delete of the images
                    $folder = $config->UPLOAD_PATH . "company/";
                    $company_image = $folder . $companyData['com_image'];
                    if (file_exists($company_image)) {
                        @unlink($company_image);
                    }
                }
            }
        } else {
            $com_id = $prdtArray;
            $companyData = $this->fetchEntryById($com_id);
            if (count($companyData) > 0) {
                $data = array('com_status' => '2');
                $this->update($data, 'com_id =' . $com_id);
                //delete of the image
                $folder = $config->UPLOAD_PATH . "company/";
                $company_image = $folder . $companyData['com_image'];
                if (file_exists($company_image)) {
                    @unlink($company_image);
                }
            }
        }
    }

}
